<?php
    require_once("include/game_core.php");
    
    if (isset($_POST['submit']))  { 
        require_once(VALIDATION_FILE);
        
        // Process Form
        $uname = $_POST['user'];
        $pass = $_POST['pass'];
        $email = $_POST['email'];
        $nick = $_POST['nick'];
        $avatar_num = $_POST['avatar_combo'];
       
        $error = false;
        
        // validate input, additionally to the JS in the reg page.
        if ( Validation::ValidateRegisteration($uname,$pass,$email,$nick) ) {
            //validation ok, continue to execute registration
            require_once(DB_FILE);
            require_once(USER_CONTROLLER_FILE);
            $db = new DBConnection();   // make new connection
            $user_cont = new UserController($db); // make new user controller
            if ( $user_cont->executeRegister($uname, $pass, $email, $nick, ($avatar_num+0) ) ) { // try Reg
                // reg success!
                $_SESSION['new_user_reg'] = 1;
                header( 'Location: reg_comp.php' ) ;
            }
            else {
                $error = $user_cont->getErrorMSG();
            }
        }
        else {
            $error = Validation::getErrorMSG();
        }
    }
    require_once(VIEW_BASE_FILE);
    $view = new view_base();
    $view->ShowHeader();
?>
<link href="css/register.css" rel="stylesheet" type="text/css" media="all" />
<script src="js/register.js"></script>
<?php
    $view->ShowMenu("none");
    $view->ShowConatinerHead();
?>
<div id="content">
    <h2>New User Registration</h2>
    <p>Your're just a few steps away from glory!</p>
    <div id="ErrorMSG"><?php
        if (isset ($error) && strlen($error)> 0 ) {
            echo '<div id="ErrorDisc">' . $error . '</div>';
        } 
    ?></div>
    <form method="post" action="" id="user_reg" name="user_reg" onsubmit="return validateForm();">
    <p>
        <h3>Login Information</h3>
        <table border ="0" width="100%">
            <tr>
                <td  width="30%">Username:</td><td><input type="text" name="user" id="user" maxlength="30" autocomplete="off"/></td>
            </tr>
            <tr>
                <td>Email:</td><td><input type="text" name="email" id="email" maxlength="100" autocomplete="off"/></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td>
                    <input type="password" name="pass" id="pass" maxlength="60" onkeyup="passwordStrength(this.value);" autocomplete="off"/>
                </td>
                <td>
                    <div id="passwordDescription"><div id="pass_color_0">Password not entered</div></div>
                </td>
            </tr>
            <tr>
                <td>Confirm Password:</td><td><input type="password" name="pass2" id="pass2" maxlength="60" autocomplete="off"/></td>
            </tr>
        </table>
    </p>
    <p>
    <h3>User Information</h3>
        <table border ="0" width="100%">
            <tr>
                <td width="30%">Nick Name:</td><td><input type="text" name="nick" id="nick" maxlength="20" autocomplete="off"/></td>
            </tr>
            <tr>
                <td>Avatar:</td>
                <td>
                    <table>
                        <tr><td align="center"><img id="imageToSwap" src="images/avatars/0.png" /></td></tr>
                        <tr><td>
                            <?php require(VIEW_REGISTER_AVATAR_COMBO); ?>
                        </td></tr>
                    </table>
                </td>
            </tr>                                      
        </table>
    </p>
    <p><input type="submit" name="submit" id="submit" value="Register"></p>
    </form>	
</div>
<?php include (VIEW_SIDE_FILE); ?>
<?php   $view->ShowFooter();      ?>